<?
class philhealth {

    // Author: Herman Tolentino MD
    // CHITS Project 2004

    function philhealth() {
        //
        // do not forget to update version
        //
        $this->author = 'Herman Tolentino MD';
        $this->version = "0.1-".date("Y-m-d");
        $this->module = "philhealth";
        $this->description = "CHITS Module - Philhealth";
    }

    // --------------- STANDARD MODULE FUNCTIONS ------------------

    function init_deps() {
    //
    // insert dependencies in module_dependencies
    //
        module::set_dep($this->module, "module");
        module::set_dep($this->module, "patient");
        module::set_dep($this->module, "healthcenter");
        module::set_dep($this->module, "family");
        module::set_dep($this->module, "ptgroup");
    }

    function init_lang() {
    //
    // insert necessary language directives
    //

        module::set_lang("FTITLE_PHILHEALTH_CARD", "english", "PHILHEALTH CARD FORM", "Y");
        module::set_lang("FTITLE_PHILHEALTH_CARD_REGISTRATION", "english", "PHILHEALTH CARD REGISTRATION", "Y");
        module::set_lang("INSTR_LOOKUP_PATIENT", "english", "INSTRUCTIONS: Look up patient name you want to register by using the form below.", "Y");
        module::set_lang("INSTR_PHILHEALTH_CARD", "english", "INSTRUCTIONS: Fill in the following form with the correct values for this patient.", "Y");
        module::set_lang("FTITLE_PHILHEALTH_RECORD_FOR", "english", "PHILHEALTH RECORD FOR ", "Y");
        module::set_lang("THEAD_PHILHEALTH_NUMBER", "english", "PHILHEALTH ID", "Y");
        module::set_lang("THEAD_EXPIRY_DATE", "english", "EXPIRY DATE", "Y");

    }

    function init_menu() {
        if (func_num_args()>0) {
            $arg_list = func_get_args();
            $module_id = $arg_list[0];
        }
        module::set_menu($this->module, "Philhealth Card", "PATIENTS", "_philhealth_patient");
        module::set_menu($this->module, "Philhealth Report", "STATS", "_philhealth_report");

        // put in more details
        module::set_detail($this->description, $this->version, $this->author, $this->module);
    }

    function init_stats() {
    }

    function init_help() {
    }

    function init_sql() {
        if (func_num_args()>0) {
            $arg_list = func_get_args();
            $module_id = $arg_list[0];
        }

        module::execsql("CREATE TABLE `m_patient_philhealth` (".
            "`philhealth_id` varchar(50) NOT NULL default '',".
            "`healthcenter_id` varchar(10) NOT NULL default '0',".
            "`patient_id` int(11) NOT NULL default '0',".
            "`philhealth_timestamp` timestamp(14) NOT NULL,".
            "`expiry_date` date NOT NULL default '0000-00-00',".
            "PRIMARY KEY  (`philhealth_id`,`patient_id`,`expiry_date`)".
            ") TYPE=InnoDB; ");

        module::execsql("CREATE TABLE `m_consult_philhealth_services` (".
            "`consult_id` float NOT NULL default '0',".
            "`philhealth_timestamp` timestamp(14) NOT NULL,".
            "`user_id` int(11) NOT NULL,".
            "`philhealth_id` varchar(50) NOT NULL default '',".
            "`blood_count` char(1) NOT NULL default '',".
            "`urinalysis` char(1) NOT NULL default '',".
            "`fecalysis` char(1) NOT NULL default '',".
            "`sputum_microscopy` char(1) NOT NULL default '',".
            "`chest_xray` char(1) NOT NULL default '',".
            "`cervical_cancer_screening` char(1) NOT NULL default '',".
            "`regular_bp_measurement` char(1) NOT NULL default '',".
            "`annual_digital_rectal` char(1) NOT NULL default '',".
            "`body_measurement` char(1) NOT NULL default '',".
            "`periodic_breast_exam` char(1) NOT NULL default '',".
            "`smoking_counselling` char(1) NOT NULL default '',".
            "`lifestyle_modification` char(1) NOT NULL default '',".
            "PRIMARY KEY  (`consult_id`)".
            ") TYPE=InnoDB; ");
    }

    function drop_tables() {
        module::execsql("DROP TABLE `m_patient_philhealth`;");
        module::execsql("DROP TABLE `m_consult_philhealth_services`;");
    }

    // --------------- CUSTOM MODULE FUNCTIONS ------------------

    function form_philhealth() {
    //
    // form for registering patients for philhealth cards
    // Medicare Para Sa Masa
    //
        if (func_num_args()>0) {
            $arg_list = func_get_args();
            $menu_id = $arg_list[0];
            $post_vars = $arg_list[1];
            $get_vars = $arg_list[2];
            $validuser = $arg_list[3];
            $isadmin = $arg_list[4];
            print_r($arg_list);
            if ($get_vars["patient_id"] && $get_vars["philhealth_id"]) {
                $sql = "select philhealth_id, expiry_date from m_patient_philhealth ".
                       "where patient_id = '".$get_vars["patient_id"]."' and philhealth_id = '".$get_vars["philhealth_id"]."'";
                if ($result = mysql_query($sql)) {
                    if (mysql_num_rows($result)) {
                        $card = mysql_fetch_array($result);
                    }
                }
            }
        }
        print "<table width='400'>";
        print "<form action = '".$_SERVER["SELF"]."?page=".$get_vars["page"]."&menu_id=".$get_vars["menu_id"]."&patient_id=".$get_vars["patient_id"]."' name='form_philhealth_card' method='post'>";
        print "<tr valign='top'><td>";
        $patient_name = patient::get_name($get_vars["patient_id"]);
        print "<span class='patient'><font color='black'>".strtoupper($patient_name)."</font></span><br><br>";
        print "</td></tr>";
        print "<tr valign='top'><td>";
        print "<b>".INSTR_PHILHEALTH_CARD."</b><br><br>";
        print "</td></tr>";
        print "<tr valign='top'><td>";
        print "<span class='boxtitle'>PHILHEALTH CARD NUMBER</span><br/>";
        print "<input type='text' class='textbox' size='25' maxlength='50' name='philhealth_id' value='".($card["philhealth_id"]?$card["philhealth_id"]:$post_vars["philhealth_id"])."' style='border: 1px solid #000000'><br>";
        print "<input type='hidden' name='patient_id' value='".$get_vars["patient_id"]."' />";
        print "</td></tr>";
        print "<tr valign='top'><td>";
        print "<span class='boxtitle'>EXPIRY DATE</span><br/>";
        print "<input type='text' class='textbox' size='10' maxlength='10' name='expiry_date' value='".($card["expiry_date"]?$card["expiry_date"]:$post_vars["expiry_date"])."' style='border: 1px solid #000000'><br>";
        print "<small>Use the format MM/DD/YYYY.</small><br/>";
        print "</td></tr>";
        print "<tr><td><br>";
        if ($get_vars["philhealth_id"]) {
            print "<input type='hidden' name='philhealth_id' value='".$get_vars["philhealth_id"]."'>";
            //print "<input type='submit' value = 'Update Card' class='textbox' name='submitcard' style='border: 1px solid #000000'> ";
            print "<input type='submit' value = 'Delete Card' class='textbox' name='submitcard' style='border: 1px solid #000000'> ";
        } else {
            print "<input type='submit' value = 'Add Card' class='textbox' name='submitcard' style='border: 1px solid #000000'> ";
        }
        print "</td></tr>";
        print "</form>";
        print "</table><br>";
    }

    function process_card() {
        if (func_num_args()>0) {
            $arg_list = func_get_args();
            $menu_id = $arg_list[0];
            $post_vars = $arg_list[1];
            $get_vars = $arg_list[2];
            $validuser = $arg_list[3];
            $isadmin = $arg_list[4];
            //print_r($arg_list);
        }
        switch ($post_vars["submitcard"]) {
        case "Add Card":
            if ($post_vars["philhealth_id"] && $post_vars["expiry_date"] && $post_vars["patient_id"]) {
                list($month,$day,$year) = explode("/", $post_vars["expiry_date"]);
                $expiry_date = $year."-".str_pad($month, 2, "0", STR_PAD_LEFT)."-".str_pad($day, 2, "0", STR_PAD_LEFT);
                $sql = "insert into m_patient_philhealth (philhealth_id, healthcenter_id, patient_id, philhealth_timestamp, expiry_date) ".
                       "values ('".$post_vars["philhealth_id"]."', '".$_SESSION["datanode"]["code"]."', '".$post_vars["patient_id"]."', sysdate(), '$expiry_date')";
                $result = mysql_query($sql);
            }
            break;
        case "Delete Card":
            print $sql = "delete from m_patient_philhealth where philhealth_id = '".$post_vars["philhealth_id"]."' and patient_id = '".$post_vars["patient_id"]."'";
            $result = mysql_query($sql);
            break;
        }
        header("location: ".$_SERVER["PHP_SELF"]."?page=".$get_vars["page"]."&menu_id=".$get_vars["menu_id"]."&patient_id=".$get_vars["patient_id"]);
    }

    function _philhealth() {
        if (func_num_args()>0) {
            $arg_list = func_get_args();
            $menu_id = $arg_list[0];
            $post_vars = $arg_list[1];
            $get_vars = $arg_list[2];
            $validuser = $arg_list[3];
            $isadmin = $arg_list[4];
            //print_r($arg_list);
        }
        print "<span class='patient'>".FTITLE_PHILHEALTH_CARD_REGISTRATION."</span><br/><br/>";
        if ($post_vars["submitconsult"]) {
            // consult id here is actually patient id...
            header("location: ".$_SERVER["PHP_SELF"]."?page=".$get_vars["page"]."&menu_id=".$get_vars["menu_id"]."&patient_id=".$post_vars["consult_id"]);
        }
        if ($post_vars["submitsearch"]) {
            patient::process_search($menu_id, $post_vars, $get_vars);
        }
        if ($post_vars["submitcard"]) {
            philhealth::process_card($menu_id, $post_vars, $get_vars);
        }
        if ($get_vars["patient_id"]) {
            philhealth::form_philhealth($menu_id, $post_vars, $get_vars);
            philhealth::display_philhealth($menu_id, $post_vars, $get_vars);
        } else {
            print "<b>".INSTR_LOOKUP_PATIENT."</b><br/><br/>";
            patient::newsearch($menu_id, $post_vars, $get_vars);
        }

    }

    function display_philhealth() {
        if (func_num_args()>0) {
            $arg_list = func_get_args();
            $menu_id = $arg_list[0];
            $post_vars = $arg_list[1];
            $get_vars = $arg_list[2];
            $validuser = $arg_list[3];
            $isadmin = $arg_list[4];
            //print_r($arg_list);
        }
        $patient_name = patient::get_name($get_vars["patient_id"]);
        print "<table width='400'>";
        print "<tr valign='top'><td colspan='3'>";
        print "<span class='patient'>".FTITLE_PHILHEALTH_RECORD_FOR." ".strtoupper($patient_name)."</span><br>";
        print "</td></tr>";
        print "<tr valign='top'><td><b>".THEAD_NAME."</b></td><td><b>".THEAD_PHILHEALTH_NUMBER."</b></td><td><b>".THEAD_EXPIRY_DATE."</b></td></tr>";
        $sql = "select h.patient_id, concat(p.patient_lastname, ', ',p.patient_firstname, ' ', p.patient_middle), philhealth_id, expiry_date ".
               "from m_patient_philhealth h, m_patient p ".
               "where h.patient_id = p.patient_id order by h.expiry_date desc";
        if ($result = mysql_query($sql)) {
            if (mysql_num_rows($result)) {
                while (list($pid, $name, $hid, $expiry) = mysql_fetch_array($result)) {
                    print "<tr valign='top'><td>$name</td><td><a href='".$_SERVER["SELF"]."?page=".$get_vars["page"]."&menu_id=$menu_id&patient_id=$pid&philhealth_id=$hid'>$hid</a></td><td>$expiry</td></tr>";
                }
            }
        }
        print "</table><br>";
    }

    function _patient_philhealth() {
        if (func_num_args()>0) {
            $arg_list = func_get_args();
            $menu_id = $arg_list[0];
            $post_vars = $arg_list[1];
            $get_vars = $arg_list[2];
            $validuser = $arg_list[3];
            $isadmin = $arg_list[4];
            //print_r($arg_list);
        }
        // always check dependencies
        if ($exitinfo = $this->missing_dependencies('healthcenter')) {
            return print($exitinfo);
        }
        print "<table width='600'><tr valign='top'><td width='50%'>";
        philhealth::form_services($menu_id, $post_vars, $get_vars);
        print "</td><td>";
        if ($post_vars["submitservices"]) {
            philhealth::process_services($menu_id, $post_vars, $get_vars);
            header("location: ".$_SERVER["PHP_SELF"]."?page=".$get_vars["page"]."&menu_id=".$get_vars["menu_id"]."&consult_id=".$get_vars["consult_id"]."&ptmenu=".$get_vars["ptmenu"]."&module=".$get_vars["module"]);
        }
        healthcenter::show_visitdetails($menu_id, $post_vars, $get_vars);
        print "</td></tr></table>";

    }

    function show_services() {
    }

    function process_services() {
        if (func_num_args()>0) {
            $arg_list = func_get_args();
            $menu_id = $arg_list[0];
            $post_vars = $arg_list[1];
            $get_vars = $arg_list[2];
            $validuser = $arg_list[3];
            $isadmin = $arg_list[4];
            print_r($arg_list);
        }
        switch($post_vars["submitservices"]) {
        case "Clear Services":
            $sql_delete = "delete from m_consult_philhealth_services where consult_id = '".$post_vars["consult_id"]."'";
            $result_delete = mysql_query($sql_delete);
            break;
        case "Save Services":
            if ($post_vars["philhealth_id"] && $post_vars["consult_id"]) {
                $blood_count = ($post_vars["blood_count"]?"Y":"N");
                $urinalysis = ($post_vars["urinalysis"]?"Y":"N");
                $fecalysis = ($post_vars["fecalysis"]?"Y":"N");
                $sputum_microscopy = ($post_vars["sputum_microscopy"]?"Y":"N");
                $chest_xray = ($post_vars["chest_xray"]?"Y":"N");
                $acetic_acid = ($post_vars["acetic_acid"]?"Y":"N");
                $reg_bp = ($post_vars["reg_bp"]?"Y":"N");
                $annual_rectal = ($post_vars["annual_rectal"]?"Y":"N");
                $body_measurement = ($post_vars["body_measurement"]?"Y":"N");
                $breast_exam = ($post_vars["breast_exam"]?"Y":"N");
                $smoking_cessation = ($post_vars["smoking_cessation"]?"Y":"N");
                $lifestyle_mod = ($post_vars["lifestyle_mod"]?"Y":"N");
                $sql = "insert into m_consult_philhealth_services (consult_id, philhealth_timestamp, user_id, ".
                       "philhealth_id, blood_count, urinalysis, fecalysis, sputum_microscopy, ".
                       "chest_xray, cervical_cancer_screening, regular_bp_measurement, ".
                       "annual_digital_rectal, body_measurement, periodic_breast_exam, ".
                       "smoking_counselling, lifestyle_modification) ".
                       "values ('".$get_vars["consult_id"]."', sysdate(), '".$post_vars["philhealth_id"]."', '".$_SESSION["userid"]."', ".
                       "'$blood_count', '$urinalysis', '$fecalysis', '$sputum_microscopy', ".
                       "'$chest_xray', '$acetic_acid', '$reg_bp', '$annual_rectal', ".
                       "'$body_measurement', '$breast_exam', '$smoking_cessation', '$lifestyle_mod')";
                $result = mysql_query($sql);
            }
            break;
        }
    }

    function get_philhealth_id() {
        if (func_num_args()>0) {
            $arg_list = func_get_args();
            $patient_id = $arg_list[0];
        }
        $sql = "select philhealth_id from m_philhealth_card where patient_id = '$patient_id'";
        if ($result  = mysql_query($sql)) {
            if (mysql_num_rows($result)) {
                list($id) = mysql_fetch_array($result);
                return $id;
            }
        }
    }

    function whois_philhealth_member() {
        if (func_num_args()>0) {
            $arg_list = func_get_args();
            $family_id = $arg_list[0];
        }
        $sql = "select f.patient_id, p.patient_lastname, p.patient_firstname, h.philhealth_id ".
               "from m_family_members f, m_patient p, m_patient_philhealth h ".
               "where f.patient_id = p.patient_id and h.patient_id = f.patient_id ".
               "and f.family_id = '$family_id'";
        if ($result = mysql_query($sql)) {
            if (mysql_num_rows($result)) {
                while (list($pid, $last, $first, $phid) = mysql_fetch_array($result)) {
                    $member_array[] .= "$pid|$phid|$last, $first";
                }
                return $member_array;
            }
        }
    }

    function form_services() {
        if (func_num_args()>0) {
            $arg_list = func_get_args();
            $menu_id = $arg_list[0];
            $post_vars = $arg_list[1];
            $get_vars = $arg_list[2];
            $validuser = $arg_list[3];
            $isadmin = $arg_list[4];
            //print_r($arg_list);
        }
        print "<table width='300'>";
        print "<form action = '".$_SERVER["SELF"]."?page=".$get_vars["page"]."&menu_id=$menu_id&consult_id=".$get_vars["consult_id"]."&ptmenu=".$get_vars["ptmenu"]."&module=".$get_vars["module"]."' name='form_vitalsigns' method='post'>";
        print "<tr valign='top'><td>";
        print "<b>".INSTR_CORRECT_VALUES."</b><br/><br/>";
        print "</td></tr>";
        print "<tr valign='top'><td>";
        // PHILHEALTH ID
        $patient_id = healthcenter::get_patient_id($get_vars["consult_id"]);
        if ($phid = philhealth::get_philhealth_id($get_vars["patient_id"])) {
            print "<b>PHILHEALTH ID:</b> $phid";
        } else {
            // is patient dependent
            // look for family member who is philhealth member
            $family_id = family::get_family_id($patient_id);
            if ($members = philhealth::whois_philhealth_member($family_id)) {
                print "<b>PHILHEALTH MEMBER(S) IN FAMILY:</b><br/>";
                if (count($members)>0) {
                    foreach($members as $key=>$value) {
                        list($pid, $phid, $name) = explode("|", $value);
                        print "<input type='radio' name='philhealth_id' value='$phid'/> $name ($phid)<br/>";
                    }
                    print "<br/>";
                }
            }
        }
        $sql = "select blood_count, urinalysis, fecalysis, sputum_microscopy, ".
               "chest_xray, cervical_cancer_screening, regular_bp_measurement, ".
               "annual_digital_rectal, body_measurement, periodic_breast_exam, ".
               "smoking_counselling, lifestyle_modification from ".
               "m_consult_philhealth_services where ".
               "consult_id = '".$get_vars["consult_id"]."'";
        if ($result = mysql_query($sql)) {
            if (mysql_num_rows($result)) {
                $service = mysql_fetch_array($result);
            }
        }
        print "<input type='hidden' name='consult_id' value='".$get_vars["consult_id"]."'/>";
        print "</td></tr>";
        print "<tr valign='top'><td>";
        print "<table width='300' cellpadding='2' cellspacing='0' bgcolor='#CCFFCC' style='border: 1px solid black'>";
        print "<tr valign='top'><td>";
        print "<b>PREVENTIVE SERVICES:</b><br/>";
        print "</td></tr>";
        if (ptgroup::is_female(patient::get_gender(healthcenter::get_patient_id($get_vars["consult_id"])))) {
            print "<tr valign='top'><td>";
            print "<input type='checkbox' name='acetic_acid' ".($service["cervical_cancer_screening"]?"checked":"")." value='1'/> Acetic acid screening for cervical cancer<br/>";
            print "</td></tr>";
        }
        print "<tr valign='top'><td>";
        print "<input type='checkbox' name='reg_bp' ".($service["regular_bp_measurement"]?"checked":"")." value='1'/> Regular BP measurement<br/>";
        print "</td></tr>";
        print "<tr valign='top'><td>";
        print "<input type='checkbox' name='annual_rectal' ".($service["annual_digital_rectal"]?"checked":"")." value='1'/> Annual digital rectal exam<br/>";
        print "</td></tr>";
        print "<tr valign='top'><td>";
        print "<input type='checkbox' name='body_measurement' ".($service["body_measurement"]?"checked":"")." value='1'/> Body measurement<br/>";
        print "</td></tr>";
        print "<tr valign='top'><td>";
        print "<input type='checkbox' name='breast_exam' ".($service["periodic_breast_exam"]?"checked":"")." value='1'/> Periodic clinical breast exam<br/>";
        print "</td></tr>";
        print "<tr valign='top'><td>";
        print "<input type='checkbox' name='smoking_cessation' ".($service["smoking_counselling"]?"checked":"")." value='1'/> Counselling for smoking cessation<br/>";
        print "</td></tr>";
        print "<tr valign='top'><td>";
        print "<input type='checkbox' name='lifestyle_mod' ".($service["lifestyle_modification"]?"checked":"")." value='1'/> Lifestyle modification advisory<br/>";
        print "</td></tr>";
        print "</table>";
        print "</td></tr>";
        print "<tr valign='top'><td>";
        print "<table width='300' cellpadding='2' cellspacing='0' bgcolor='#FFCC99' style='border: 1px solid black'>";
        print "<tr valign='top'><td>";
        print "<b>LAB EXAMS:</b><br/>";
        print "</td></tr>";
        print "<tr valign='top'><td>";
        print "<input type='checkbox' name='blood_count' ".($service["blood_count"]?"checked":"")." value='1'/> Complete Blood Count<br/>";
        print "</td></tr>";
        print "<tr valign='top'><td>";
        print "<input type='checkbox' name='urinalysis' ".($service["urinalysis"]?"checked":"")." value='1'/> Urinalysis<br/>";
        print "</td></tr>";
        print "<tr valign='top'><td>";
        print "<input type='checkbox' name='fecalysis' ".($service["fecalysis"]?"checked":"")." value='1'/> Fecalysis<br/>";
        print "</td></tr>";
        print "<tr valign='top'><td>";
        print "<input type='checkbox' name='sputum_microscopy' ".($service["sputum_microscopy"]?"checked":"")." value='1'/> Sputum microscopy<br/>";
        print "</td></tr>";
        print "<tr valign='top'><td>";
        print "<input type='checkbox' name='chest_xray' ".($service["chest_xray"]?"checked":"")." value='1'/> Chest X-Ray<br/>";
        print "</td></tr>";
        print "</table>";
        print "</td></tr>";
        print "<tr><td>";
        print "<br><input type='submit' value = 'Save Services' class='textbox' name='submitservices' style='border: 1px solid #000000'> ";
        print "<input type='submit' value = 'Clear Services' class='textbox' name='submitservices' style='border: 1px solid #000000'><br>";
        print "</td></tr>";
        print "</form>";
        print "</table><br>";
    }

}
?>
